import os


class TelData(object):
    def __init__(self):
        path = os.getcwd()
        f = open(path + '\\number.txt', 'r', encoding='utf-8')
        file_contents = f.readlines()
        self.tel_book = [[0 for i in range(4)]
                         for j in range(len(file_contents))]
        for i in range(0, len(file_contents)):
            content = file_contents[i].split(" ")
            self.tel_book[i] = content
        f.close()

    def new(self, tel_info):
        self.tel_book.append(tel_info)

    def search(self, name):
        result = []
        for content in self.tel_book:
            if (name in content[0]):
                result.append(content)
        return result

    def save_tel_book(self):
        path = os.getcwd()
        with open(path + '\\number.txt', 'a', encoding='utf-8') as f:
            f.seek(0)  #定位光标到文件的开头
            f.truncate()  #清空文件
            temp = []
            for content in self.tel_book:
                temp.append(
                    (" ").join(content).replace("\n", ""))  #替换掉每行末尾的换行符，下面统一添加
            f.write("\n".join(temp))


""" teldata = TelData()
newperson = ["2", "lisi", "13511576516", "sdddddd"]
print(teldata.tel_book)
teldata.tel_book.append(newperson)
teldata.save_tel_book(teldata.tel_book) """